package com.marakana.yamba4;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import com.chris.weibo.R;

public class TimeLineActivity2 extends BaseActivity {

	Cursor cursor;
	ListView listTimeLine;
	SimpleCursorAdapter adapter;
	static final String[] FROM = { DbHelper.C_CREATED_AT, DbHelper.C_USER,
			DbHelper.C_TEXT };
	static final int[] TO = { R.id.textCreatedAt, R.id.textuser, R.id.textText };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_timeline);

		if (yamba.getPrefs().getString("username", null) == null) {
			startActivity(new Intent(this, PreferenceActivity.class));
			Toast.makeText(this, R.string.msgSetupPrefs, Toast.LENGTH_LONG)
					.show();
		}

		listTimeLine = (ListView) findViewById(R.id.listTimeLine);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		this.setupList();
	}

	private void setupList() {
		// 从数据库中读取数据
		cursor = yamba.getStatusData().getStatusUpdates();
		startManagingCursor(cursor);

		// 设置Adapter
		adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, FROM, TO);
		listTimeLine.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		yamba.getStatusData().close();
	}

}
